<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="采购处理" v-model="visible" align-center width="800px"
    :z-index="2000" @close="handleClose">
    <el-form label-width="100px" label-position="top">
      <el-form-item label="处理方式">
        <el-radio v-model="form.process_result" :value="1">系统自动处理</el-radio>
        <!-- <el-radio v-model="form.process_result" :label="2">采购手动操作</el-radio> -->
      </el-form-item>
    </el-form>
    <div class="text-sm">{{ message }}</div>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit()">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
import { resSkip } from "@/utils/res"
const load = ref(false)
const visible = ref(false)
let loadings = reactive({})
const message = ref()
const form = ref({})

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (pdc_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = { pdc_id, process_result: 1 }
  const res = await api.getMessage({ pdc_id })
  if (res.message) {
    ElMessage.error(res.message)
    return
  }
  message.value = res.data.message
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = (is_skip = 1) => {
  loadings.submit = true
  form.value.is_skip = is_skip
  api.purchaseProcess(form.value).then(res => {
    resSkip({
      res,
      success: () => {
        emit('success')
        close()
      },
      submit: () => {
        submit(2)
      },
    })
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>